-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
waddrmgr: fix lock order in importScriptAddress #958
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
@@ -2324,6 +2321,20 @@ func (s *ScopedKeyManager) importScriptAddress(ns walletdb.ReadWriteBucket, | |||
// Add the new managed address to the cache of recent addresses and | |||
// return it. | |||
s.addrs[addrKey(scriptIdent)] = managedAddr | |||
|
|||
if updateStartBlock { | |||
// Now that the database has been updated, update the start block in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: line is slightly over 80 characters if a tabulator size/width of 8 spaces is used.
// Now that the database has been updated, update the start block in | ||
// memory too if needed. Ensure the manager lock goes outside the scoped | ||
// manager lock. | ||
s.mtx.Unlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're releasing the lock here, it's possible for a second/parallel call to importScriptAddress
to come in and potentially progress to the if bs.Height < s.rootManager.syncState.startBlock.Height
check before the below is updated, similar to how we could end up with duplicated addresses in lightningnetwork/lnd#8697. BUT, since we're just increasing the start block, the worst that could happen is that this write would happen twice. So IMO an acceptable solution until we do a full rewrite of this logic (which I agree is definitely necessary at some point).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM❤️ Thanks for the fix!🍺
First of all, the code between the scoped manager and the root manager needs a full rewrite. The lock orders are unmanageable. This PR fixes a potential deadlock (seen on Breez), but it doesn't fix the underlying issue here. The main problem is the locks between scoped manager and the root manager are taken in different orders. Sometimes root manager first, then scoped manager, sometimes the other way around.
For this PR the decision is to never lock the root manager inside a scoped manager lock. This PR only fixes a single occurence of this issue, there are more occurences elsewhere.
This PR has been in production for a swap server for Breez for a few months now and fixes our deadlock occurrences.